//
//  CalendarAnimation.swift
//  YOKO
//
//  Created by Zoe_G on 16/5/10.
//  Copyright © 2016年 15Tec. All rights reserved.
//

import UIKit

class CalendarAnimation: NSObject{
    
    static var height: CGFloat?
    
    static let weekLabelHeight: CGFloat = 24.0
    
    static let dateLabelHeight: CGFloat = 30.0
    
    static var duration: NSTimeInterval! = 0.2

    static func executeForwardsAnimation(containerViewConrtoller: UIViewController, fromView:UIView, toView: UIView, openUpHeight: CGFloat){
        
        height = openUpHeight
        
        let containerView = containerViewConrtoller.view
        containerView.addSubview(toView)
        
        let upRegion = CGRectMake(0, 0, fromView.frame.width, height!)
        let upView = fromView.resizableSnapshotViewFromRect(upRegion, afterScreenUpdates: false, withCapInsets: UIEdgeInsetsZero)
        upView.frame = upRegion
        containerView.addSubview(upView)
        
        let downRegion = CGRectMake(0, height!, fromView.frame.width, fromView.frame.height)
        let downView = fromView.resizableSnapshotViewFromRect(downRegion, afterScreenUpdates: false, withCapInsets: UIEdgeInsetsZero)
        downView.frame = downRegion
        containerView.addSubview(downView)
        
        toView.frame = CGRectOffset(toView.frame, 0, height!-40)
        
        UIView.animateWithDuration(self.duration, delay: 0.0, options: .CurveEaseOut, animations: {
            upView.frame = CGRectOffset(upView.frame, 0, -upView.frame.height)
            downView.frame = CGRectOffset(downView.frame, 0, downView.frame.height)
            
            toView.frame = CGRectOffset(toView.frame, 0, -height!+40)
            
            }, completion: {(finished: Bool) in
                
                upView.removeFromSuperview()
                downView.removeFromSuperview()
                
        })
    
    }
    
    static func executeReverseAnimation(containerViewConrtoller: UIViewController, fromView:UIView, toView: UIView){
        
        let containerView = containerViewConrtoller.view
        
        containerView.addSubview(toView)
        
        let upRegion = CGRectMake(0, 0, toView.frame.width, height!)
        let upView = toView.resizableSnapshotViewFromRect(upRegion, afterScreenUpdates: false, withCapInsets: UIEdgeInsetsZero)
        upView.frame = CGRectOffset(upRegion, 0, -height!)
        containerView.addSubview(upView)
        
        let downRegion = CGRectMake(0, height!, toView.frame.width, toView.frame.height)
        let downView = toView.resizableSnapshotViewFromRect(downRegion, afterScreenUpdates: false, withCapInsets: UIEdgeInsetsZero)
        downView.frame = CGRectOffset(downRegion, 0, downView.frame.height)
        containerView.addSubview(downView)
        
        fromView.frame = CGRectOffset(fromView.frame, 0, 0)
        
        UIView.animateWithDuration(self.duration, delay: 0.0, options: .CurveEaseOut, animations: {
            //目标位置
            upView.frame = upRegion
            downView.frame = downRegion
            
            fromView.frame = CGRectOffset(fromView.frame, 0, height!-40)
            
            }, completion: {(finished: Bool) in
                
                upView.removeFromSuperview()
                downView.removeFromSuperview()
                
                //containerView.addSubview(toView)
                fromView.frame = CGRectOffset(fromView.frame, 0, -height!+40)
                
                
                
        })

    }
    
}
